package com.cnzz.site1255174697.utils;

import android.app.Activity;
import android.content.Intent;

import com.alibaba.baichuan.android.trade.AlibcTrade;
import com.alibaba.baichuan.android.trade.AlibcTradeSDK;
import com.alibaba.baichuan.android.trade.callback.AlibcTradeCallback;
import com.alibaba.baichuan.android.trade.constants.AlibcConstants;
import com.alibaba.baichuan.android.trade.model.AlibcShowParams;
import com.alibaba.baichuan.android.trade.model.OpenType;
import com.alibaba.baichuan.android.trade.model.TradeResult;
import com.alibaba.baichuan.android.trade.page.AlibcBasePage;
import com.alibaba.baichuan.android.trade.page.AlibcMyCartsPage;
import com.alibaba.baichuan.android.trade.page.AlibcPage;
import com.cnzz.site1255174697.activity.WebItemIdActivity;
import com.cnzz.site1255174697.activity.WebViewActivity;

import java.util.HashMap;
import java.util.Map;

/**
 * 页面打开工具
 */
public class AlibcTradeUtils {


    /**
     * 原生打开Url
     *
     * @param activity
     * @param taokeUrl
     */
    public static void showNativeUrl(Activity activity, String taokeUrl) {
        //提供给三方传递配置参数
        Map<String, String> exParams = new HashMap<>();
        exParams.put(AlibcConstants.ISV_CODE, "appisvcode");
        AlibcTradeSDK.setForceH5(true);
        //实例化URL打开page
        AlibcBasePage page = new AlibcPage(taokeUrl);
        //设置页面打开方式
        AlibcShowParams showParams = new AlibcShowParams(OpenType.H5, false);
        //使用自己的Activity & webview打开detail
        AlibcTrade.show(activity, page, showParams, null, exParams,
                new AlibcTradeCallback() {
                    @Override
                    public void onTradeSuccess(TradeResult tradeResult) {
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                    }
                });
    }

    /**
     * 自定义Web加载
     *
     * @param activity
     * @param taokeUrl
     */
    public static void showUrl(final Activity activity, String taokeUrl) {
        activity.startActivity(new Intent(activity, WebViewActivity.class).putExtra("url", taokeUrl));
    }

    /**
     * 通过Id
     *
     * @param activity
     * @param itemId
     */
    public static void showItemId(final Activity activity, String itemId) {
        activity.startActivity(new Intent(activity, WebItemIdActivity.class).putExtra("url", itemId));
    }

    /**
     * 购物车
     *
     * @param activity
     */
    public static void showShopCart(Activity activity) {
        //提供给三方传递配置参数
        Map<String, String> exParams = new HashMap<>();
        exParams.put(AlibcConstants.ISV_CODE, "appisvcode");
        AlibcTradeSDK.setForceH5(true);
        //实例化我的购物车打开page
        AlibcBasePage myCartsPage = new AlibcMyCartsPage();
        //设置页面打开方式
        AlibcShowParams showParams = new AlibcShowParams(OpenType.H5, false);
        //使用自己的Activity & webview打开detail
        AlibcTrade.show(activity, myCartsPage, showParams, null, exParams,
                new AlibcTradeCallback() {
                    @Override
                    public void onTradeSuccess(TradeResult tradeResult) {
                    }

                    @Override
                    public void onFailure(int code, String msg) {
                    }
                });
    }
}
